package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;

import model.EduDegree;
import dao.EduDegreeDAO;


public class EduDegreeDAOImpl implements EduDegreeDAO {
	private DBUtil dbu = new DBUtil();
	private Connection conn = null;
	private PreparedStatement pstat = null;
	private ResultSet rs = null;
	private boolean result=false;
	private String sql=null;
	public List<EduDegree> getAll() {
		List <EduDegree> list=new ArrayList<EduDegree>();
		try {
			conn = dbu.getConnection();
			String sql="select * from EduDegree";
			pstat=conn.prepareStatement(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				EduDegree edd = new EduDegree();
				edd.setId(rs.getInt("id"));
				edd.setName(rs.getString("name"));
				list.add(edd);
			}
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return list;
	}

	public EduDegree getById(int id) {
		conn= dbu.getConnection();
		EduDegree degree=new EduDegree();
		try {
			pstat=conn.prepareStatement("select * from EduDegree where id="+id);
			rs=	pstat.executeQuery();
			while(rs.next()){
			degree.setId(rs.getInt("id"));
			degree.setName(rs.getString("name"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbu.closeConnection(conn, pstat, rs);
		}
		return degree;
	}

	public EduDegree getByName(String name) {
		conn= dbu.getConnection();
		EduDegree edd = new EduDegree();
		try {
			pstat=conn.prepareStatement("select (id,name) from EduDegree where name="+name);
			rs=	pstat.executeQuery();
			edd.setId(rs.getInt("id"));
			edd.setName(rs.getString("name"));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbu.closeConnection(conn, pstat, rs);
		}
		return edd;
	}

}
